// 基于template/src/widgets/\{{appEntities}}/\{{ctrls@GRID}}-grid/\{{spinalCase ctrl.codeName}}-grid-model.ts.hbs生成
{{#* inline 'initColumn' column grid}}
{
	align: '{{lowerCase column.align}}',
{{#and grid.aggMode (neq grid.aggMode 'NONE') (neq column.aggMode 'NONE')}}
	aggMode: '{{column.aggMode}}',
	aggField: '{{#if column.aggField}}{{lowerCase column.aggField}}{{else}}{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}{{/if}}',
	aggValueFormat: `{{column.aggValueFormat}}`,
{{/and}}
	codeName: '{{column.codeName}}',
	caption: '{{column.caption}}',
	captionLanResTag: '{{#if column.capPSLanguageRes}}{{column.capPSLanguageRes.lanResTag}}{{/if}}',
	cellCssName: '{{#if column.cellPSSysCss}}{{column.cellPSSysCss.cssName}}{{/if}}',
	columnType: '{{column.columnType}}',
{{#if column.psSysImage}}
  {{#if column.psSysImage.cssClass}}
  cssClass: "{{column.psSysImage.cssClass}}",
  {{/if}}
{{/if}}    
{{#if column.psAppCodeList}}
  codeListTag: '{{column.psAppCodeList.codeListTag}}',
{{/if}}
{{#if column.psSysPFPlugin}}
  pluginCode: '{{column.psSysPFPlugin.pluginCode}}',
{{/if}}
	dataItemName: '{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}',
	dataIndex: '{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}',
{{#if column.enableSort}}
  sorter: true,
{{/if}}
	enableRowEdit: {{column.enableRowEdit}},
{{#if grid.psDEGridEditItems}}
	{{#each grid.psDEGridEditItems as | editItem |}}
		{{#eq editItem.codeName column.codeName}}
			{{#if editItem.psEditor}}
	editorType: '{{editItem.psEditor.editorType}}',
	editorStyle: '{{editItem.psEditor.editorStyle}}',
			{{/if}}
			{{#if editItem.resetItemName}}
	resetItemName: '{{editItem.resetItemName}}',
			{{/if}}
			{{#if editItem.valueItemName}}
	valueItemName: '{{editItem.valueItemName}}',
			{{/if}}
			{{#if editItem.psDEGridEditItemUpdate}}
	itemUpdate: {
		details: [{{#if editItem.psDEGridEditItemUpdate.psDEGEIUpdateDetails}}{{#each editItem.psDEGridEditItemUpdate.psDEGEIUpdateDetails as | updateDetail |}}'{{updateDetail.name}}'{{#unless @last}}, {{/unless}}{{/each}}{{/if}}],
				{{#if editItem.psDEGridEditItemUpdate.customCode}}
		scriptCode: `{{editItem.psDEGridEditItemUpdate.scriptCode}}`,
				{{/if}}
		method: '{{#if editItem.psDEGridEditItemUpdate.psAppDEMethod}}{{editItem.psDEGridEditItemUpdate.psAppDEMethod.codeName}}{{/if}}',
		showBusyIndicator: {{#if editItem.psDEGridEditItemUpdate.showBusyIndicator}}true{{else}}false{{/if}}
	},
			{{/if}}
		{{/eq}}
	{{/each}}
{{/if}}
	enableSort: {{column.enableSort}},
	excelCaption: '{{column.excelCaption}}',
	headerCssName: '{{#if column.headerPSSysCss}}{{column.headerPSSysCss.cssName}}{{/if}}',
	hideDefault: {{column.hideDefault}},
	hiddenDataItem: {{column.hiddenDataItem}},
	name: '{{column.name}}',
{{#and column.enableLinkView column.linkPSAppView}}
	linkView: '{{column.linkPSAppView.codeName}}',
	{{#if column.linkValueItem}}
	linkValueItem: '{{column.linkValueItem}}',
	{{/if}}
	{{#if column.linkPSAppView.psAppDataEntity}}
	linkViewDeKeyField: '{{lowerCase column.linkPSAppView.psAppDataEntity.codeName}}',
	{{/if}}
{{/and}}
{{#eq column.widthUnit 'PX'}}
  width: {{column.width}},
{{else}}
  {{#gte column.width 1}}
  minWidth: {{column.width}},
  {{/gte}}
  width: 150,
{{/eq}}
{{#eq column.columnType 'UAGRIDCOLUMN'}}
	fixed: 'right',
{{else}}
	resizable: true,
{{/eq}}
{{#if column.psDEUIAction}}
	uIAction: { uIActionTag: '{{column.psDEUIAction.uIActionTag}}' },
{{/if}}
{{#if column.psAppDEField}}
	stdDataType: {{column.psAppDEField.stdDataType}},
	{{#neq column.psAppDEField.precision 0}}
	precision: {{column.psAppDEField.precision}},
	{{/neq}}
{{/if}}
	valueFormat: '{{#if column.valueFormat}}{{column.valueFormat}}{{else if (and column.psAppDEField column.psAppDEField.valueFormat (neq column.psAppDEField.valueFormat '%1$s'))}}{{column.psAppDEField.valueFormat}}{{/if}}',
{{#and (eq column.columnType 'UAGRIDCOLUMN') column.psDEUIActionGroup column.psDEUIActionGroup.psUIActionGroupDetails}}
  	uIActionDetails: [
  {{#each column.psDEUIActionGroup.psUIActionGroupDetails as | a |}}
    	{
				name: "{{a.name}}",
				caption: "{{a.psUIAction.caption}}",
				showIcon: {{a.showIcon}},
				showCaption: {{a.showCaption}},
				separator: {{a.addSeparator}},
				uIActionTag: "{{a.psUIAction.uIActionTag}}",
				noPrivDisplayMode: {{a.psUIAction.noPrivDisplayMode}},
				dataAccessAction: '{{a.psUIAction.dataAccessAction}}',
				{{#if a.psUIAction.tooltip}}
				tooltip: App.ts('{{#if a.psUIAction.tooltipPSLanguageRes}}{{a.psUIAction.tooltipPSLanguageRes.lanResTag}}{{/if}}', '{{a.psUIAction.tooltip}}'),
				{{/if}}
				actionTarget: '{{a.psUIAction.actionTarget}}',
				uIActionMode: "{{a.psUIAction.uIActionMode}}",
				disabled: false,
				visible: true,
			{{#if grid.psAppViewLogics}}
					{{#each grid.psAppViewLogics as | logic |}}
							{{#eq logic.name (concat grid.name '_' column.codeName '_' a.name '_click')}}
				logic: {
					name: '{{logic.name}}',
					actionTag: '{{a.psUIAction.uIActionTag}}',
					{{#if a.psUIAction.psAppDataEntity}}
					actionDECodeName: '{{a.psUIAction.psAppDataEntity.codeName}}',
					{{else}}
						{{#and logic.psAppViewUIAction.psUIAction logic.psAppViewUIAction.psUIAction.psAppDataEntity}}
					actionDECodeName: '{{logic.psAppViewUIAction.psUIAction.psAppDataEntity.codeName}}',
						{{else}}
					actionDECodeName: '{{grid.psAppDataEntity.codeName}}',
						{{/and}}
					{{/if}}
					predefinedType: '{{#if logic.psAppViewUIAction.psUIAction}}{{logic.psAppViewUIAction.psUIAction.predefinedType}}{{/if}}'
				},
							{{/eq}}
					{{/each}}
			{{/if}}
			{{#if a.psUIAction.psSysImage}}
					{{#if a.psUIAction.psSysImage.imagePath}}
				imgPath: "{{a.psUIAction.psSysImage.imagePath}}",
					{{/if}}
					{{#if a.psUIAction.psSysImage.cssClass}}
				iconClass: "{{a.psUIAction.psSysImage.cssClass}}",
					{{/if}}
			{{/if}}
    	},
    {{/each}}
  	],
{{/and}}
{{#and (eq column.columnType 'GROUPGRIDCOLUMN') column.psDEGridColumns}}
  	children: [
  	{{#each column.psDEGridColumns as | children |}}
    	{{> initColumn column=children grid=grid}}
  	{{/each}}
  	]
{{/and}}
},
{{/inline}}
{{#* inline 'getEmpty' columns}}
{{#each columns as | column |}}{{#and (eq column.widthUnit 'STAR') (neq column.hideDefault true)}}hasResult{{/and}}{{#and (eq column.columnType 'GROUPGRIDCOLUMN') column.psDEGridColumns}}{{> getEmpty columns=column.psDEGridColumns}}{{/and}}{{/each}}
{{/inline}}
import { IGridModel, IParam, isEmpty, VerifyUtil } from '@/core';

function getModel(): IGridModel {
	return {
    cssName: "{{#if ctrl.psSysCss}}{{ctrl.psSysCss.cssName}}{{/if}}",        
		aggMode: '{{ctrl.aggMode}}',
{{#eq ctrl.aggMode 'ALL'}}
		aggDeCodeName: '{{ctrl.aggPSAppDataEntity.codeName}}',
		aggDataSet: '{{ctrl.aggPSAppDEDataSet.codeName}}',
{{/eq}}
		caption: '{{ctrl.caption}}',
		controlType: '{{ctrl.controlType}}',
		codeName: '{{ctrl.codeName}}',
    singleSelect:{{#if ctrl.singleSelect}}true{{else}}false{{/if}},        
		columns: getGridColumns(),
		dataItems: getGridDataItems(),
		dataExport: getDataExport(),
		enableRowEdit: {{ctrl.enableRowEdit}},
		entityCodeName: '{{ctrl.psAppDataEntity.codeName}}',
		entityLogicName: '{{ctrl.psAppDataEntity.logicName}}',
		createDefaultItems: getCreateDefaultItems(),
    updateDefaultItems: getUpdateDefaultItems(),
{{#if ctrl.psNavigateContexts}}
		navigateContext: {{> @macro/common/navparam.hbs appNavParams=ctrl.psNavigateContexts}},
{{/if}}
{{#if ctrl.psNavigateParams}}
		navigateParams: {{> @macro/common/navparam.hbs appNavParams=ctrl.psNavigateParams}},
{{/if}}
{{#and ctrl.enableGroup (neq ctrl.groupMode 'NONE')}}
		enableGroup: true,
		//  请确保此项与表格分组列模型中的dataIndex一致
		groupDataIndex: 'grid_group',
		groupMode: '{{ctrl.groupMode}}',
		groupField: '{{lowerCase ctrl.groupPSAppDEField.codeName}}',
	{{#each ctrl.psDEGridColumns as | column |}}
		{{#and column.psAppDEField (eq column.psAppDEField.codeName ctrl.groupPSAppDEField.codeName) column.psAppCodeList}}
		groupFieldCodeListTag: '{{column.psAppCodeList.codeListTag}}',
		{{/and}}
	{{/each}}
  	{{#eq ctrl.groupMode 'CODELIST'}}
    groupCodeListTag: '{{ctrl.groupPSCodeList.codeListTag}}',
  	{{/eq}}
{{/and}}
		name: '{{ctrl.name}}',
		noSort: {{ctrl.noSort}},
		rules: {
			//	静态值规则
			staticRules: {
{{#each ctrl.psDEGridEditItemVRs as | ruleItem |}}
  				{{#each ctrl.psDEGridColumns as | column |}}{{#eq column.codeName ruleItem.psDEGridEditItemName}}{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}{{/eq}}{{/each}}: [
  	{{#eq ruleItem.valueRuleType 'SYSVALUERULE'}}
        			{
    	{{#eq ruleItem.psSysValueRule.ruleType 'REG'}}
						trigger: ['change', 'blur'],
						pattern: /{{ruleItem.psSysValueRule.regExCode}}/,
						message:'{{ruleItem.psSysValueRule.ruleInfo}}'
		{{/eq}}
    	{{#eq ruleItem.psSysValueRule.ruleType 'SCRIPT'}}
						validator: (rule: any, value: any, callback: any, source: any) => {
							if (isEmpty(value)) {
								return Promise.resolve();
							}
							try {
								eval(`{{ruleItem.psSysValueRule.scriptCode}}`);
							} catch (error: any) {
								App.getNotificationHelper().error('错误', '校验脚本值规则异常');
							}
							return Promise.resolve();
						}
    	{{/eq}}
        			},
  	{{/eq}}
  	{{#eq ruleItem.valueRuleType 'DEFVALUERULE'}}
    	{{#each ctrl.psDEGridEditItems as | editItem |}}
      		{{#eq editItem.codeName ruleItem.psDEGridEditItemName}}
					{
						validator: (rule: any, value: any, callback: any, source: any) => {
							if (!source || isEmpty(source['{{#if editItem.valueItemName}}{{editItem.valueItemName}}{{else if editItem.dataItemName}}{{editItem.dataItemName}}{{else}}{{ruleItem.psDEGridEditItemName}}{{/if}}'])) {
								return Promise.resolve();
							}
							const { isPast, infoMessage } = VerifyUtil.verifyDeRules(
								'{{#if editItem.valueItemName}}{{editItem.valueItemName}}{{else if editItem.dataItemName}}{{editItem.dataItemName}}{{else}}{{ruleItem.psDEGridEditItemName}}{{/if}}',
								source,
								{{> @macro/common/derules.hbs item=ruleItem.psDEFValueRule.psDEFVRGroupCondition}}
							);
							if (!isPast) {
								return Promise.reject(infoMessage || '{{ruleItem.psDEFValueRule.ruleInfo}}');
							}
							return Promise.resolve();
						}
					},
    		{{/eq}}
    	{{/each}}
  	{{/eq}}
      			],
{{/each}}
			},
			//	编辑项值规则
			editRules: [
{{#each ctrl.psDEGridEditItems as | editItem |}}
	{{#and editItem.psEditor (neq editItem.psEditor.editorType 'HIDDEN')}}
				{
					name: '{{#each ctrl.psDEGridColumns as | column |}}{{#eq column.codeName editItem.codeName}}{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}{{/eq}}{{/each}}',
					caption: '{{editItem.caption}}',
					allowEmpty: {{editItem.allowEmpty}},
					editor: {
						editorType: '{{editItem.psEditor.editorType}}',
		{{#if editItem.psEditor.maxValue}}
						maxValue: {{editItem.psEditor.maxValue}},
		{{/if}}
		{{#if editItem.psEditor.minValue}}
						minValue: {{editItem.psEditor.minValue}},
		{{/if}}
		{{#if editItem.psEditor.showMaxLength}}
						maxLength: {{editItem.psEditor.maxLength}},
		{{/if}}
		{{#if editItem.psEditor.minLength}}
						minLength: {{editItem.psEditor.minLength}},
		{{/if}}
		{{#if editItem.psEditor.precision}}
						precision: {{editItem.psEditor.precision}},
		{{/if}}
		{{#if editItem.psEditor.psSysValueRule}}
						getPSSysValueRule: {
							ruleType: '{{editItem.psEditor.psSysValueRule.ruleType}}',
			{{#eq editItem.psEditor.psSysValueRule.ruleType 'REG'}}
							regExCode: /{{editItem.psEditor.psSysValueRule.regExCode}}/,
			{{/eq}}
							ruleInfo: {{editItem.psEditor.psSysValueRule.ruleInfo}}
						}
		{{/if}}
					}
				}{{#unless @last}},{{/unless}}
	{{/and}}
{{/each}}
			]
    	},
		showHeader: {{#if ctrl.hideHeader}}false{{else}}true{{/if}},
		sortDir: '{{ctrl.minorSortDir}}',
		sortField: '{{#if ctrl.minorSortPSAppDEField}}{{lowerCase ctrl.minorSortPSAppDEField.codeName}}{{/if}}',
		limit: {{#if ctrl.pagingSize}}{{ctrl.pagingSize}}{{else}}20{{/if}},
{{#if ctrl.psDEDataImport}}
		dataImport: {
			codeName: '{{ctrl.psDEDataImport.codeName}}',
			ignoreError: {{ctrl.psDEDataImport.ignoreError}},
	{{#if ctrl.psDEDataImport.psDEDataImportItems}}
			importItems: [
		{{#each ctrl.psDEDataImport.psDEDataImportItems as | importItem |}}
				{
					name: '{{lowerCase importItem.name}}',
					caption: App.ts('{{#if importItem.capPSLanguageRes}}{{importItem.capPSLanguageRes.lanResTag}}{{/if}}', '{{importItem.caption}}'),
					{{#if importItem.psCodeList}}
					codeListTag: '{{importItem.psCodeList.codeListTag}}',
					{{/if}}
					{{#if importItem.psDEField}}
					fieldCodeName: '{{lowerCase importItem.psDEField.codeName}}'
					{{/if}}
				}{{#unless @last}},{{/unless}}
		{{/each}}
			]
	{{/if}}
		},
{{/if}}
{{#if ctrl.enablePagingBar}}
		paging: {
			defaultPageSize: {{#if ctrl.pagingSize}}{{ctrl.pagingSize}}{{else}}20{{/if}},
		}
{{/if}}
  	}
}
//  获取所有表格列模型
function getGridColumns():IParam[] {
  return [
{{#and ctrl.enableGroup (neq ctrl.groupMode 'NONE')}}
		//  表格分组列模型
		{
			caption: "分组",
			align: "left",
			columnType: "GROUP",
			width: 100,
			resizable: true,
			ellipsis: true,
			dataIndex: "grid_group",
		},
{{/and}}
{{#if ctrl.psDEGridColumns}}
	{{#each ctrl.psDEGridColumns as | column |}}
		{{!-- 操作列放在最后一列 --}}
		{{#neq column.columnType 'UAGRIDCOLUMN'}}
		{{> initColumn column=column grid=ctrl}}
		{{/neq}}
  	{{/each}}
{{/if}}
{{#neq (getEmpty columns=ctrl.psDEGridColumns) 'hasResult'}}
		{
			caption: "",
			columnType: "EMPTYCOLUMN",
			dataIndex: "autowidthcolumn",
			align: "center",
			resizable: true,
			width: 0,
			ellipsis: true,
			aggMode: "NONE"
		},
{{/neq}}
{{#if ctrl.psDEGridColumns}}
	{{#each ctrl.psDEGridColumns as | column |}}
		{{!-- 操作列放在最后一列 --}}
		{{#eq column.columnType 'UAGRIDCOLUMN'}}
		{{> initColumn column=column grid=ctrl}}
		{{/eq}}
  {{/each}}
{{/if}}
  ];
}

//  获取所有表格数据项
function getGridDataItems() {
  return [
{{#each ctrl.psDEGridDataItems as | dataItem |}}
    {
			name: '{{name}}',
			codeName: '{{lowerCase codeName}}',
    {{#if psAppDEField}}
			fieldCodeName: '{{lowerCase psAppDEField.codeName}}',
			fieldName: '{{lowerCase psAppDEField.name}}',
			valueFormat: '{{psAppDEField.valueFormat}}',
			dataType: '{{psAppDEField.stdDataType}}'
    {{/if}}
    },
{{/each}}
  ];
}

function getDataExport() {
{{#if ctrl.psDEDataExport}}
//	获取导出项
	{{!-- 输出导出项 --}}
	return {
		codeName: '{{ctrl.psDEDataExport.codeName}}',
		fileName: '{{ctrl.psAppDataEntity.logicName}}',
	{{#if ctrl.psDEDataExport.maxRowCount}}
		maxRowCount: {{ctrl.psDEDataExport.maxRowCount}},
	{{/if}}
	{{#if ctrl.psDEDataExport.enableBackend}}
		enableBackend: {{ctrl.psDEDataExport.enableBackend}},
	{{/if}}
		enableFront: {{ctrl.psDEDataExport.enableFront}},
		exportItems: [
	{{#if ctrl.psDEDataExport.psDEDataExportItems}}
		{{#each ctrl.psDEDataExport.psDEDataExportItems as | item |}}
			{
				name: '{{lowerCase item.name}}',
				label: '{{item.caption}}',
				labelLanResTag: '{{#if item.capPSLanguageRes}}{{item.capPSLanguageRes.lanResTag}}{{/if}}',
			{{#if item.psCodeList}}
				codeListTag: '{{item.psCodeList.codeListTag}}',
			{{/if}}
			{{#if item.psDEField}}
				{{#and field.valueFormat (neq field.valueFormat '%1$s')}}
				valueFormat: '{{field.valueFormat}}',
				{{/and}}
			{{/if}}
				show: true
			}{{#unless @last}},{{/unless}}
		{{/each}}
	{{/if}}
		]
	};
{{else}}
	return {
		codeName: 'default',
		enableFront: true,
		fileName: '{{ctrl.psAppDataEntity.logicName}}',
		exportItems: [
{{#each ctrl.psDEGridColumns as | column |}}
	{{#eq column.columnType 'DEFGRIDCOLUMN'}}
			{
				name: '{{#if column.dataItemName}}{{lowerCase column.dataItemName}}{{else}}{{lowerCase column.codeName}}{{/if}}',
				label: '{{column.caption}}',
  				labelLanResTag: '{{#if column.capPSLanguageRes}}{{column.capPSLanguageRes.lanResTag}}{{/if}}',
				dataType: {{column.psAppDEField.stdDataType}},
		{{#if column.psAppCodeList}}
				codeListTag: '{{column.psAppCodeList.codeListTag}}',
		{{/if}}
		{{#if column.valueFormat}}
				valueFormat: '{{column.valueFormat}}',
		{{/if}}
				show: {{#if column.hideDefault}}false{{else}}true{{/if}}
			}{{#unless @last}},{{/unless}}
	{{/eq}}
{{/each}}
		]
	}
{{/if}}
}

function getCreateDefaultItems() {
	return [
{{#each ctrl.psDEGridEditItems as | editItem |}}
  	{{#if (and editItem.codeName (or editItem.createDV editItem.createDVT))}}
		{
			createDV: "{{editItem.createDV}}",
			createDVT: "{{editItem.createDVT}}",
			{{#if editItem.dataItemName}}
			property: "{{lowerCase editItem.dataItemName}}",
			{{else}}
			property: "{{lowerCase editItem.codeName}}",
			{{/if}}
			{{#if (and editItem.psEditor editItem.psEditor.dateTimeFormat)}}
			valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
			{{else if editItem.psAppDEField}}
			valueFormat: "{{editItem.psAppDEField.valueFormat}}",
			{{/if}}
			{{#if editItem.psAppDEField}}
			dataType: "{{editItem.psAppDEField.stdDataType}}"
			{{/if}}
		}{{#unless @last}},{{/unless}}
  	{{/if}}
{{/each}}
	]
}

function getUpdateDefaultItems() {
	return [
{{#each ctrl.psDEGridEditItems as | editItem |}}
  	{{#if (and editItem.codeName (or editItem.updateDV editItem.updateDVT))}}
		{
			updateDV: "{{editItem.updateDV}}",
			updateDVT: "{{editItem.updateDVT}}",
			{{#if editItem.dataItemName}}
			property: "{{lowerCase editItem.dataItemName}}",
			{{else}}
			property: "{{lowerCase editItem.codeName}}",
			{{/if}}
			{{#if (and editItem.psEditor editItem.psEditor.dateTimeFormat)}}
			valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
			{{else if editItem.psAppDEField}}
			valueFormat: "{{editItem.psAppDEField.valueFormat}}",
			{{/if}}
			{{#if editItem.psAppDEField}}
			dataType: "{{editItem.psAppDEField.stdDataType}}"
			{{/if}}
			}{{#unless @last}},{{/unless}}
  	{{/if}}
{{/each}}
	];
}

export const model = getModel();